/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;

namespace Xceed.Silverlight.Data
{
  internal abstract class DataEnumeratorBase : IDataEnumerator
  {
    #region DataChanged Event

    public event EventHandler<DataChangedEventArgs> DataChanged;

    protected void RaiseDataReplaced( object oldItem, object newItem )
    {
      if( this.DataChanged != null )
      {
        this.DataChanged( 
          this, 
          new DataChangedEventArgs( 
            DataChangedAction.Replace, 
            newItem, 
            oldItem ) );
      }
    }

    protected void RaiseDataAdd( object newItem )
    {
      if( this.DataChanged != null )
      {
        this.DataChanged( 
          this, 
          new DataChangedEventArgs( 
            DataChangedAction.Add, 
            newItem ) );
      }
    }

    protected void RaiseDataRemoved( object oldItem, SourcePosition removePosition, SourcePosition newCurrentPosition )
    {
      if( this.DataChanged != null )
      {
        this.DataChanged( 
          this, 
          new DataChangedEventArgs(
            DataChangedAction.Remove, 
            oldItem, 
            removePosition, 
            newCurrentPosition ) );
      }
    }

    protected void RaiseDataReset()
    {
      if( this.DataChanged != null )
      {
        this.DataChanged( this, new DataChangedEventArgs( DataChangedAction.Reset ) );
      }
    }

    protected void RaiseDataItemChanged( ItemChangedInfo itemChangedInfo )
    {
      if( this.DataChanged != null )
      {
        this.DataChanged( this, new DataChangedEventArgs( itemChangedInfo ) );
      }
    }

    protected void RaiseDataChanged( DataChangedEventArgs args )
    {
      if( this.DataChanged != null )
      {
        this.DataChanged( this, args );
      }
    }

    #endregion DataChanged Event


    protected void CheckMoveAndFetchParameters( long expectedMoveCount, int expectedFetchCount )
    {
      if( expectedMoveCount == 0 && expectedFetchCount == 0 )
        throw new InvalidOperationException( "expectedMoveCount and expectedFetchCount must be non-zero" );
    }


    #region IDisposable Members

    public virtual void Dispose()
    {
      this.Dispose( true );
      GC.SuppressFinalize( this );
    }

    protected virtual void Dispose( bool disposing )
    {
      if( disposing )
      {
        this.DataChanged = null;
      }
    }

    ~DataEnumeratorBase()
    {
      this.Dispose( false );
    }

    #endregion IDisposable Members
  }
}
